﻿#include "picturelayer.h"
PictureLayer::PictureLayer(QWidget *parent, const Qt::AlignmentFlag &v_alignment, const Qt::AlignmentFlag &h_alignment, const QPoint &pos)
    : StackWindow(parent, v_alignment, h_alignment, pos)
{
    //setMouseTracking(true);  // 启用鼠标跟踪，这样可以获取移动事件
    helicopter = new Helicopter(this,(":/image/Helicopter.svg"));
    elementVector.append((Sprite*)helicopter->markerVector[helicopter->markerVector.size()-1]);
    helicopter->hide();
    helicopter->textEdit->setParent(this);
    helicopter->textEdit->setFixedSize(200,200);
    helicopter->textEdit->move(300,300);
    helicopter->lineEdit->setParent(this);
    helicopter->lineEdit->setFixedSize(200,80);
    helicopter->lineEdit->move(600,300);
    connect(this,&PictureLayer::generateEnemyPlaneSignal,[=]{
        generateEnemyPlane();
        initElementVector();
    });
    connect(this,&PictureLayer::generateEnemyTankSignal,[=]{
        generateEnemyTank();
        initElementVector();
    });
    connect(this,&PictureLayer::generateEnemyBunkerSignal,[=]{
        generateEnemyBunker();
        initElementVector();
    });
    connect(this,&PictureLayer::generateFriendDroneSignal,[=]{
        generateFriendDrone();
        initElementVector();
    });
    connect(this,&PictureLayer::generatePlaneMarkerSignal,[=](){
        this->helicopter->generatePlaneMarker();
        elementVector.append((Sprite*)helicopter->markerVector[helicopter->markerVector.size()-1]);
        initElementVector();
        helicopter->markerVector[helicopter->markerVector.size()-1]->position=mousePos.toPoint();
    });
    //setAttribute(Qt::WA_NoSystemBackground, true);
}

void PictureLayer::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    helicopter->move(helicopter->location+helicopter->temp.toPoint());
    helicopter->move(helicopter->location+temp.toPoint());

    if(helicopter->isMarked)
        helicopter->show();
    else
        helicopter->hide();
    for(int i=0;i<elementVector.size();i++){
        if(elementVector[i]->isMarked==true){
            elementVector[i]->move(elementVector[i]->location+elementVector[i]->temp.toPoint());
            elementVector[i]->widget->move(elementVector[i]->location+elementVector[i]->temp.toPoint()+QPoint(-74,elementVector[i]->size().height()));
            elementVector[i]->show();
        }
        else
            elementVector[i]->hide();
    }
    removeVector();
    QPen pen;
    pen.setColor(Qt::yellow);
    pen.setWidth(3);
    painter.setPen(pen);
    if(helicopter->showRoute){
        for(int i=0;i<helicopter->markerVector.size()-1;i++){
            painter.drawLine(helicopter->markerVector[i]->position+helicopter->markerVector[i]->temp.toPoint(),helicopter->markerVector[i+1]->position+helicopter->markerVector[i+1]->temp.toPoint());
            //qDebug()<<(helicopter->markerVector[0]->position+helicopter->markerVector[0]->temp.toPoint()).x()<<" "<<(helicopter->markerVector[0]->position+helicopter->markerVector[0]->temp.toPoint()).y();
        }
        pen.setColor(Qt::red);
        pen.setWidth(3);
        painter.setPen(pen);
        for (int i = 0; i < helicopter->markerVector.size() - 1; i++) {
            QPoint startPoint = helicopter->markerVector[i]->position;
            QPoint endPoint = helicopter->markerVector[i + 1]->position;
            QPoint midPoint = (startPoint + endPoint) / 2;
            double radLat1 = ((startPoint.y()-400)*0.000065+23) * M_PI / 180.0;
            double radLon1 = ((startPoint.x()-600)*0.000071666666+113)* M_PI / 180.0;
            double radLat2 = ((endPoint.y()-400)*0.000065+23)* M_PI / 180.0;
            double radLon2 = ((startPoint.y()-600)*0.000071666666+113)* M_PI / 180.0;

            double deltaLat = radLat2 - radLat1;
             double deltaLon = radLon2 - radLon1;
             double a = qSin(deltaLat / 2) * qSin(deltaLat / 2) +
                        qCos(radLat1) * qCos(radLat2) *
                        qSin(deltaLon / 2) * qSin(deltaLon / 2);
             double c = 2 * qAtan2(qSqrt(a), qSqrt(1 - a));
             double distance = 6371 * c;  // 地球的平均半径约为6371公里
            QString distanceText = QString::number(distance*1.6)+"km";
            // 绘制标记数字
            painter.drawText(midPoint+helicopter->markerVector[i]->temp.toPoint(),distanceText);
        }
    }


}
void PictureLayer::generateEnemyPlane()
{
    EnemyPlane *e =new EnemyPlane(this,":/image/enemyPlane.png");
    elementVector.append((Sprite*)e);
}
void PictureLayer::generateEnemyTank()
{
    EnemyPlane *e =new EnemyPlane(this,":/image/enemyTank.png");
    elementVector.append((Sprite*)e);
}
void PictureLayer::generateEnemyBunker()
{
    EnemyPlane *e =new EnemyPlane(this,":/image/enemyBunker.png");
    elementVector.append((Sprite*)e);
}

void PictureLayer::removeVector()
{
    for(int i=0;i<elementVector.size();i++){
        if(elementVector[i]->isExist==false){
            elementVector[i]->widget->close();
            elementVector[i]->closeButton->close();
            elementVector[i]->close();
            delete elementVector[i];
            elementVector.remove(i);
            i--;
        }
    }

    for(int i=0;i<helicopter->markerVector.size();i++){
        if(i>=1&&helicopter->markerVector[i]->isExist==false){
            helicopter->markerVector.remove(i);
            i--;
        }
        if(i==0&&helicopter->markerVector[i]->isExist==false){
            helicopter->temp=QPointF(0,0);
            helicopter->translate=QPointF(0,0);
        }
    }

    for(int i=0;i<responseVector.size();i++){
        if(responseVector[i]->isExist==false){
            responseVector.remove(i);
            i--;
        }
    }
}
void PictureLayer::generateFriendDrone()
{
    FriendDrone *f =new FriendDrone(this,":/image/friendDrone.png");
    elementVector.append((Sprite*)f);
}

void PictureLayer::mousePressEvent(QMouseEvent *event) {
    //在这里可以处理鼠标左键按下事件
    delta=QPointF(0,0);
    mousePos=event->pos();
    isPress=true;
    if(paintingStatus==1){
        temp=QPointF(0,0);
        translatePos=QPoint(0,0);
        helicopter->location=mousePos.toPoint()+QPoint(-20,-40);
        helicopter->isMarked=true;//
        helicopter->temp=QPointF(0,0);
        helicopter->translate=QPoint(0,0);
        helicopter->isMarked=true;
        helicopter->markerVector[0]->temp=QPoint(0,0);
        helicopter->markerVector[0]->translate=QPoint(0,0);
        helicopter->markerVector[0]->position=mousePos.toPoint();
        helicopter->markerVector[0]->location=helicopter->location;
        helicopter->markerVector[0]->isMarked=true;
        helicopter->markerVector[0]->isExist=true;
    }

    if(paintingStatus==11){
        emit generateEnemyPlaneSignal();
    };

    if(paintingStatus==12){
        emit generateEnemyTankSignal();
    };

    if(paintingStatus==13){
        emit generateEnemyBunkerSignal();
    };

    if(paintingStatus==21){
        emit generateFriendDroneSignal();
    };
    if(paintingStatus==40){
        emit generatePlaneMarkerSignal();
    }
    QDockWidget::mousePressEvent(event);
    update();
}
void PictureLayer::mouseMoveEvent(QMouseEvent *event){
    if(isPress&&event->buttons()&Qt::LeftButton){
        delta=event->pos()-mousePos;
        temp=translatePos+delta;
        for(int i=0;i<elementVector.size();i++){
            elementVector[i]->temp=elementVector[i]->translate+delta;
        }
        helicopter->temp=helicopter->translate+delta;
    }
}
void PictureLayer::mouseReleaseEvent(QMouseEvent *event){
    if (event->button() == Qt::LeftButton) {
        isPress=false;
        translatePos+=delta;
        helicopter->translate+=delta;
        for(int i=0;i<elementVector.size();i++){
            elementVector[i]->translate+=delta;
        }
    }
}

void PictureLayer::initResponseVector()
{
    if(!DroneResponse){
        // 设置随机数生成器的种子为当前时间的秒数
        qsrand(QTime::currentTime().second());
        for(int i=0;i<responseVector.size();i++){
            // 生成一个介于0和100之间的随机整数
            int x = qrand() % this->width();
            int y = qrand() % this->height();
            QPoint random(x,y);
            responseVector[i]->setParent(this);
            responseVector[i]->widget->setParent(this);
            responseVector[i]->location=random+QPoint(-responseVector[responseVector.size()-1]->size().width()/2,-responseVector[responseVector.size()-1]->size().height()/2);
            responseVector[i]->temp=QPoint(0,0);
            responseVector[i]->isMarked=true;
        }
        DroneResponse=true;
    }
}
void PictureLayer::generateResponse()
{
    qsrand(QTime::currentTime().second());
    // 生成一个介于0和100之间的随机整数
    int n = qrand() % 11;
    if(!DroneResponse){
        for(int i =0;i<n;i++){
            FriendDrone *f =new FriendDrone(this,":/image/friendDrone.png");
            responseVector.append(f);
            elementVector.append((Sprite*)f);
        }
    }
}

void PictureLayer::initElementVector()
{
    elementVector[elementVector.size()-1]->setParent(this);
    elementVector[elementVector.size()-1]->widget->setParent(this);
    elementVector[elementVector.size()-1]->location=mousePos.toPoint()+QPoint(-elementVector[elementVector.size()-1]->size().width()/2,-elementVector[elementVector.size()-1]->size().height()/2);
    elementVector[elementVector.size()-1]->temp=QPointF(0,0);
    elementVector[elementVector.size()-1]->isMarked=true;
}
